package com.example.nettyrpcdemo;

import com.example.nettyrpcdemo.core.NettyRpcServer;
import com.example.nettyrpcdemo.core.RpcService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import tools.MessageManager;

@Slf4j
@SpringBootApplication
public class ServerApplication implements CommandLineRunner, ApplicationListener<ContextClosedEvent> {

    @Autowired
    private RpcService rpcService;

    @Autowired
    private NettyRpcServer nettyRpcServer;

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        // msgId和pb消息映射关系初始化
        MessageManager.init();

        // 扫描服务提供者
        rpcService.init();

        // rpc服务提供者
        nettyRpcServer.start();
    }

    @Override
    public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
        log.warn("==============SERVER CLOSE BEGAN============");

        log.warn("==============SERVER CLOSE END============");
    }
}
